package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallStoreTagCategoryMapper;
import org.linlinjava.litemall.db.dao.TagCategoryMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;
import java.util.List;
/**
 * @Description:	标签类别管理
 * @Author:			Coach tam
 * @Company:		坚持灵活  灵活坚持
 * @CreateDate:		2021-4-11 9:29:05
 */
@Service
@Transactional
public class LitemallTagCategoryService {
    @Autowired
    private LitemallStoreTagCategoryMapper tagcategoryMapper;
    @Autowired
    private TagCategoryMapper tcMapper;

    public List<LitemallStoreTagCategory> findAllForIndex() {
        LitemallStoreTagCategoryExample example = new LitemallStoreTagCategoryExample();
        LitemallStoreTagCategoryExample.Criteria criteria = example.createCriteria();


        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(" add_time asc ");

        return tagcategoryMapper.selectByExampleSelective(example,new LitemallStoreTagCategory.Column[]{LitemallStoreTagCategory.Column.id, LitemallStoreTagCategory.Column.name});
    }

    public LitemallStoreTagCategory findById(Integer id) {
        return tagcategoryMapper.selectByPrimaryKey(id);
    }


    public void add(LitemallStoreTagCategory entity) {
        entity.setAddTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        tagcategoryMapper.insertSelective(entity);
    }

    public int updateById(LitemallStoreTagCategory entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return tagcategoryMapper.updateByPrimaryKeySelective(entity);
    }

    public List<LitemallStoreTagCategory> querySelective(String name,Integer page, Integer size, String sort, String order) {
        LitemallStoreTagCategoryExample example = new LitemallStoreTagCategoryExample();
        LitemallStoreTagCategoryExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

//        PageHelper.startPage(page, size);
        return tagcategoryMapper.selectByExample(example);
    }

    public int count() {
        LitemallStoreTagCategoryExample example = new LitemallStoreTagCategoryExample();
        example.or().andDeletedEqualTo(false);

        return (int) tagcategoryMapper.countByExample(example);
    }

    public void deleteById(Integer id) {
        tagcategoryMapper.logicalDeleteByPrimaryKey(id);
    }

    public List<LitemallStoreTagCategory> findTagcategory(String name) {
        LitemallStoreTagCategoryExample example = new LitemallStoreTagCategoryExample();
        example.or().andNameEqualTo(name).andDeletedEqualTo(false);
        return tagcategoryMapper.selectByExample(example);
    }


    public List<TagCategoryVo> findAll() {
        return tcMapper.findAll();
    }
}
